-
Notifications
You must be signed in to change notification settings - Fork 1.8k
lint on unnecessary collections #15830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
lint on unnecessary collections #15830
Conversation
Lintcheck changes for 98f1059
This comment will be updated if you push new changes |
1b7f7a8
to
a7a20c0
Compare
Some changes occurred in clippy_lints/src/doc cc @notriddle |
a8d01c4
to
da2cf18
Compare
} | ||
}) | ||
.collect() | ||
fn collect_doc_replacements(attrs: &[Attribute]) -> impl Iterator<Item = (Span, String)> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why make it more complicated when the caller collects it because it needs a Vec
? This should probably a restriction
lint.
@@ -1,3 +1,4 @@ | |||
#![allow(clippy::unnecessary_collect)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use #[expect]
to ignore a lint, and put it as close as possible to the lint emission.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bit difficult, its define_conf!
tests/ui/unnecessary_collect.stderr
Outdated
error: this collection may not be necessary. consider returning the iterator itself | ||
--> tests/ui/unnecessary_collect.rs:5:12 | ||
| | ||
LL | fn bad() -> Vec<u32> { | ||
| -------- help: perhaps: `impl Iterator<Item = u32>` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest moving the "consider ..." part of the primary message into the help message to replace the "perhaps". IIRC at least rust-analyzer only shows the help message without the code suggestion in the quick fix tab, so just "help: perhaps" would look odd. It also makes the message itself more concise which is always nice
/// ``` | ||
#[clippy::version = "1.92.0"] | ||
pub UNNECESSARY_COLLECT, | ||
pedantic, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this should be a restriction lint, at least as it's currently implemented because it seems too eager to me.
It's certainly useful in a lot of cases, but fundamentally I don't think there is anything wrong with this pattern. I get that it's for beginners, but clippy::pedantic
is also enabled by experienced users, and this already introduces quite a few #[allow]
s to the codebase.
There's also a bunch of false positives that seem fairly difficult to solve in a good way, like if the iterator has local lifetimes and can't be returned. Or if the method is part of a trait impl where the trait can't be controlled by the user in which case the signature can't be changed. The lint could be made weaker and checks could be added to avoid them, but maybe the goal is to strictly find all instances of this pattern without false negatives even if it means false positives, and you're willing to go through them manually and do some refactoring. If that's the case (which this sounds like to me), restriction sounds more fitting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ada4a was talking about how it would be good for pedantic..?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not like I'm a team member or anything though, just wanted to share my opinion^^
But yes, some of the linted cases being unsolvable is unfortunate, though pedantic lints are allowed to give more false-positives than the regular ones
da2cf18
to
98f1059
Compare
☔ The latest upstream changes (possibly 0a2eece) made this pull request unmergeable. Please resolve the merge conflicts. |
98f1059
to
b359f33
Compare
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// This might not be necessary, and its more idiomatic to simply return the iterator, giving the caller the option to use it as they see fit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// This might not be necessary, and its more idiomatic to simply return the iterator, giving the caller the option to use it as they see fit. | |
/// This might not be necessary, and it's more idiomatic to simply return the iterator, giving the caller the option to use it as they see fit. |
lints on
changelog: [
unnecessary_collect
]: create lintsee also #clippy > linting on vector returns where return is collected
name can be bikeshed